package main.leetcode.offer.secondround.from03to50;

/** 面试题56-II.数组中数字出现的次数 II */
public class ex56II {
    public static void main(String[] args) {
        System.out.println(new ex56II().singleNumber(new int[] {9, 1, 7, 9, 7, 9, 7}));
        System.out.println(new ex56II().singleNumber(new int[] {3, 4, 3, 3}));
    }

    public int singleNumber(int[] nums) {
        int res = 0, count, b = 1;
        for (int i = 0; i < 32; ++i) {
            count = 0;
            for (int num : nums) {
                if ((num & b) == b) {
                    ++count;
                }
            }
            if (count % 3 != 0) {
                res |= b;
            }
            b <<= 1;
        }
        return res;
    }
}
